import 'package:bilibili/gen/assets.gen.dart';
import 'package:bilibili/http/dao/profile_dao.dart';
import 'package:bilibili/http/model/benefit_mo.dart';
import 'package:bilibili/http/model/course_mo.dart';
import 'package:bilibili/http/model/profile_mo.dart';
import 'package:bilibili/widget/hi_benefit.dart';
import 'package:bilibili/widget/hi_blur.dart';
import 'package:bilibili/widget/hi_course.dart';
import 'package:bilibili/widget/hi_profile_header.dart';
import 'package:bilibili/widget/hi_theme.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/util/logger.dart';
import 'package:hi_net/http/core/hi_error.dart';
import 'package:lottie/lottie.dart';

import '../http/model/banner_mo.dart';
import '../util/toast.dart';
import '../widget/hi_banner.dart';

class ProfilePage extends StatefulWidget {
  const ProfilePage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _ProfilePageState();
  }
}

class _ProfilePageState extends State<ProfilePage> {
  late ScrollController _controller;

  @override
  void initState() {
    _controller = ScrollController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
          future: _loadData(),
          builder: (BuildContext context, AsyncSnapshot<ProfileMo?> snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              ProfileMo? data = snapshot.data;
              if (data == null) {
                return Container();
              } else {
                return _buildSuccess(data);
              }
            } else {
              return Center(
                child: Lottie.asset(Assets.loading),
              );
            }
          }),
    );
  }

  _buildSuccess(ProfileMo data) {
    return NestedScrollView(
        controller: _controller,
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar(
              //固定
              pinned: true,
              //展开高度
              expandedHeight: 150,
              //自由空间
              flexibleSpace: FlexibleSpaceBar(
                  centerTitle: true,
                  title: HiProfileHeader(
                    face: data.face,
                    name: data.name,
                    controller: _controller,
                  ),
                  background: Stack(
                    children: [
                      Positioned.fill(
                          child: Image.network(
                        "https://www.geekailab.com/img/beauty_camera/beauty_camera4.jpg",
                        fit: BoxFit.cover,
                      )),
                      const Positioned.fill(child: HiBlur(sigma: 20)),
                      Positioned(
                          bottom: 0,
                          left: 0,
                          right: 0,
                          child: _buildStatistic(
                              fans: data.fans,
                              favorite: data.favorite,
                              like: data.like,
                              browsing: data.browsing,
                              coin: data.coin))
                    ],
                  )),
            ),
            _buildBanner(data.bannerList),
            const SliverPadding(padding: EdgeInsets.only(top: 5)),
            _buildCourse(data.courseList),
            _buildBenefit(data.benefitList),
            const SliverToBoxAdapter(
              child: SizedBox(height: 10),
            ),
            _buildTheme(),
          ];
        },
        body: Container());
  }

  _buildBanner(List<BannerMo> list) {
    return SliverPadding(
      padding: const EdgeInsets.only(top: 5, left: 10, right: 10, bottom: 10),
      sliver: SliverToBoxAdapter(
        child: HiBanner(
            bannerHeight: 120,
            list.map((item) {
              return item;
            }).toList()),
      ),
    );
  }

  Future<ProfileMo?> _loadData() async {
    try {
      ProfileMo response = await ProfileDao.get();
      return response;
    } on NeedLogin catch (e) {
      showWarnToast(e.message);
    } on HiNetError catch (e) {
      showWarnToast(e.message);
    } catch (e) {
      logger.e(e);
    }
    return null;
  }

  _buildStatistic(
      {required int like,
      required int favorite,
      required int fans,
      required int coin,
      required int browsing}) {
    return Container(
      color: Colors.white38,
      padding: const EdgeInsets.only(top: 5, bottom: 5),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          _vSmallText(favorite, "收藏"),
          _vSmallText(like, "点赞"),
          _vSmallText(browsing, "浏览"),
          _vSmallText(coin, "金币"),
          _vSmallText(fans, "粉丝"),
        ],
      ),
    );
  }

  _vSmallText(int num, String title) {
    return Column(
      children: [
        Text(
          "$num",
          style: const TextStyle(fontSize: 12, color: Colors.black),
        ),
        Text(
          title,
          style: TextStyle(fontSize: 11, color: Colors.grey[700]),
        )
      ],
    );
  }

  _buildCourse(List<Course> courseList) {
    return SliverPadding(
      padding: const EdgeInsets.only(left: 10, right: 10),
      sliver: SliverToBoxAdapter(child: HiCourse(list: courseList)),
    );
  }

  _buildBenefit(List<Benefit> benefitList) {
    return SliverPadding(
      padding: const EdgeInsets.only(left: 10, right: 10),
      sliver: SliverToBoxAdapter(
        child: HiBeneFit(benefitList: benefitList),
      ),
    );
  }

  _buildTheme() {
    return const SliverPadding(
      padding: EdgeInsets.only(left: 10, right: 10,top: 10),
      sliver: SliverToBoxAdapter(
        child: HiTheme(),
      ),
    );
  }
}

void main() => runApp(const ProfilePageApp());

class ProfilePageApp extends StatelessWidget {
  const ProfilePageApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: ProfilePage(),
    );
  }
}
